﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 23/04/2009
// Time: 18:51
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.personal.rts
{
	public struct workoutDay
	{
		public GroupBox groupBox;
		public DateTime date;
		public int workoutNumbers;
	}
	/// <summary>
	/// Description of frmCalendar.
	/// </summary>
	public partial class frmCalendar : KryptonForm
	{
		const int DAYS_NUMBER = 42;
		csharp.personal.rts.workoutDay[] workoutDays;
		Demarcation demarcation= new Demarcation();
		Demarcations listDemarcation = new Demarcations();
		Users listUser = new Users();
		
		DateTime currentDate;
		
		public frmCalendar()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			workoutDays = new workoutDay[DAYS_NUMBER];
			initGroupBoxes();
			
			//Preenche o Menu de esportes
			
			System.Windows.Forms.ToolStripMenuItem toolStripMenuItemTal = new ToolStripMenuItem("Cisclimo");
			toolStripMenuItemTal.Click += new System.EventHandler(this.ToolStripMenuItemCiclismClick);
			
			contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
									toolStripMenuItemTal});
		}
		
		private void initGroupBoxes()
		{
			workoutDays[0].groupBox= groupBox1;
			workoutDays[1].groupBox= groupBox2;
			workoutDays[2].groupBox= groupBox3;
			workoutDays[3].groupBox= groupBox4;
			workoutDays[4].groupBox= groupBox5;
			workoutDays[5].groupBox= groupBox6;
			workoutDays[6].groupBox= groupBox7;
			
			workoutDays[7].groupBox= groupBox9;
			workoutDays[8].groupBox= groupBox10;
			workoutDays[9].groupBox= groupBox11;
			workoutDays[10].groupBox= groupBox12;
			workoutDays[11].groupBox= groupBox13;
			workoutDays[12].groupBox= groupBox14;
			workoutDays[13].groupBox= groupBox15;
			
			workoutDays[14].groupBox= groupBox17;
			workoutDays[15].groupBox= groupBox18;
			workoutDays[16].groupBox= groupBox19;
			workoutDays[17].groupBox= groupBox20;
			workoutDays[18].groupBox= groupBox21;
			workoutDays[19].groupBox= groupBox22;
			workoutDays[20].groupBox= groupBox23;
			
			workoutDays[21].groupBox= groupBox25;
			workoutDays[22].groupBox= groupBox26;
			workoutDays[23].groupBox= groupBox27;
			workoutDays[24].groupBox= groupBox28;
			workoutDays[25].groupBox= groupBox29;
			workoutDays[26].groupBox= groupBox30;
			workoutDays[27].groupBox= groupBox31;
			
			workoutDays[28].groupBox= groupBox33;
			workoutDays[29].groupBox= groupBox34;			
			workoutDays[30].groupBox= groupBox35;
			workoutDays[31].groupBox= groupBox36;
			workoutDays[32].groupBox= groupBox37;
			workoutDays[33].groupBox= groupBox38;
			workoutDays[34].groupBox= groupBox39;			
			
			workoutDays[35].groupBox= groupBox41;
			workoutDays[36].groupBox= groupBox42;			
			workoutDays[37].groupBox= groupBox43;
			workoutDays[38].groupBox= groupBox44;
			workoutDays[39].groupBox= groupBox45;
			workoutDays[40].groupBox= groupBox46;
			workoutDays[41].groupBox= groupBox47;		
			
			//Tag store the number of controls in groupbox
			for( int i=0; i < DAYS_NUMBER; i++)
			{
				KryptonLabel kryptonLabelTal = new KryptonLabel();
				//kryptonLabelTal.ContextMenuStrip = this.contextMenuStrip1;
				kryptonLabelTal.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.TitleControl;
				kryptonLabelTal.Name = "kryptonLabel1";
			   kryptonLabel1.Location = new System.Drawing.Point(63, 0);
				kryptonLabelTal.Size = new System.Drawing.Size(25, 27);
				kryptonLabelTal.Text = "+";
				kryptonLabelTal.Values.ExtraText = "";
				kryptonLabelTal.Values.Image = null;				
				kryptonLabelTal.Values.Text = "+";
				kryptonLabelTal.Click += new EventHandler(kryptonLabelTal_Click);
				
				workoutDays[i].groupBox.Controls.Add(kryptonLabelTal);
				workoutDays[i].groupBox.Tag = i.ToString();
				workoutDays[i].workoutNumbers = 1;
			}
			
			groupBoxWeek1.Tag= "0";
			groupBoxWeek2.Tag= "0";
			groupBoxWeek3.Tag= "0";
			groupBoxWeek4.Tag= "0";
			groupBoxWeek5.Tag= "0";
			groupBoxWeek6.Tag= "0";
		}
		
		
		private void FrmCalendarLoad(object sender, EventArgs e)
		{
			int i, j, quantDaysInMonth;
			int totalWeekRunning, totalWeekSwinning, totalWeekCyclism;
			
			DayOfWeek firtDayOfWeek = DayOfWeek.Sunday;
			
			DateTime hoje = DateTime.Now;
			DateTime firstDayOfMonth  = new DateTime(hoje.Year, hoje.Month, 1);
			DateTime aux = firstDayOfMonth; 
			DateTime lastMonth = hoje.AddMonths(-1);
			
			totalWeekCyclism= totalWeekRunning= totalWeekSwinning= 0;
			i = 0;
			while(aux.DayOfWeek > firtDayOfWeek)
			{
				aux= aux.AddDays(-1);
				i++;
			}
			
			//Last Month
			quantDaysInMonth = DateTime.DaysInMonth(lastMonth.Year, lastMonth.Month);
			aux= firstDayOfMonth.AddDays(-1);
			j= i-1;
			while(j >= 0)
			{
				workoutDays[j].groupBox.Text = quantDaysInMonth.ToString();
				workoutDays[j].date = aux;
				
				addWorkout(Constants.RUNNING, j, "9.3 Km");
				totalWeekRunning+= 9300;
				
				addWorkout(Constants.SWINNIG, j, "500 m");				
				totalWeekSwinning+= 500;
				
				addWorkout(Constants.BIKE, j, "20 Km");
				totalWeekCyclism+= 20000;
				
				j--;
				quantDaysInMonth--;
				aux= aux.AddDays(-1);
			}
			
			//Now
			quantDaysInMonth = DateTime.DaysInMonth(hoje.Year, hoje.Month);
			aux = firstDayOfMonth;
			j= 1;
			while(j <= quantDaysInMonth)
			{
				workoutDays[i].groupBox.Text = j.ToString();
				workoutDays[i].date = aux;
				
				if( aux == hoje)
					workoutDays[i].groupBox.BackColor = SystemColors.Info;
				else
					workoutDays[i].groupBox.BackColor = SystemColors.InactiveCaptionText;
				
				addWorkout(Constants.RUNNING, i, "9.3 Km");
				totalWeekRunning+= 9300;
				
				addWorkout(Constants.SWINNIG, i, "500 m");				
				totalWeekSwinning+= 500;
				
				addWorkout(Constants.BIKE, i, "20 Km");
				totalWeekCyclism+= 20000;
			
				if ((i+1)%7 == 0)
				{					
					addTotalWeek((int)((i+1)/7), totalWeekCyclism, totalWeekRunning, totalWeekSwinning);
					totalWeekCyclism= totalWeekRunning= totalWeekSwinning= 0;
				}
				i++;
				j++;
				aux = aux.AddDays(1);
			}
			
			//Next Month
			j=1;
			while(i < DAYS_NUMBER)
			{
				workoutDays[i].groupBox.Text = j.ToString();
				workoutDays[i].date = aux;
				
				if ((i+1)%7 == 0)
				{					
					addTotalWeek((int)((i+1)/7), totalWeekCyclism, totalWeekRunning, totalWeekSwinning);
					totalWeekCyclism= totalWeekRunning= totalWeekSwinning= 0;
				}				
				i++;
				j++;				
				aux = aux.AddDays(1);
			}
			
			listUser.fillList();
			fillComboBoxUsers();
		}
		
		private void fillComboBoxUsers()
		{
			int tam = listUser.count();
			
			for(int i=0; i < tam; i++)
				cmbUser.Items.Add(listUser.get(i));
			
			cmbUser.ValueMember = "id";
			cmbUser.DisplayMember = "name";
		}
	
		private KryptonLabel createLabel(int type, int numberOfControls, string description)
		{
			KryptonLabel lblAux = new KryptonLabel();
			lblAux.Location = new Point(3, (15 * numberOfControls) + 10);
			lblAux.Text = description;
			
			if(type == Constants.BIKE)
				lblAux.Values.Image = Image.FromFile("bike.JPG");
			else if (type == Constants.RUNNING)
				lblAux.Values.Image = Image.FromFile("corrida.JPG");
			else if (type == Constants.SWINNIG)
				lblAux.Values.Image = Image.FromFile("natacao.JPG");			
			
			return lblAux;
		}
		
		private void addWorkout(int type, int index, string description)
		{
			int numberOfControls = workoutDays[index].workoutNumbers;
			
			workoutDays[index].groupBox.Controls.Add(createLabel(type, numberOfControls,description));
			workoutDays[index].workoutNumbers++;
		}
	
		private void addTotalWeek(int index, int ciclism, int running, int swimming)
		{
			addTotalWeek(Constants.RUNNING, index, (running/1000).ToString()+" Km");
			
			addTotalWeek(Constants.SWINNIG, index, (swimming).ToString()+" m");
			
			addTotalWeek(Constants.BIKE, index, (ciclism/1000).ToString()+" Km");				
		}
		
		private void addTotalWeek(int type, int week, string description)
		{
			int numberOfControls;
			if (week == 1)
			{
				numberOfControls = Convert.ToInt32(groupBoxWeek1.Tag);
				groupBoxWeek1.Controls.Add(createLabel(type, numberOfControls, description));
				groupBoxWeek1.Tag = (numberOfControls+1).ToString();
			}
			else if (week == 2)
			{
				numberOfControls = Convert.ToInt32(groupBoxWeek2.Tag);
				groupBoxWeek2.Controls.Add(createLabel(type, numberOfControls, description));
				groupBoxWeek2.Tag = (numberOfControls+1).ToString();
			}
			else if (week == 3)
			{
				numberOfControls = Convert.ToInt32(groupBoxWeek3.Tag);
				groupBoxWeek3.Controls.Add(createLabel(type, numberOfControls, description));
				groupBoxWeek3.Tag = (numberOfControls+1).ToString();
			}
			else if (week == 4)
			{
				numberOfControls = Convert.ToInt32(groupBoxWeek4.Tag);
				groupBoxWeek4.Controls.Add(createLabel(type, numberOfControls, description));
				groupBoxWeek4.Tag = (numberOfControls+1).ToString();
			}
			else if (week == 5)
			{
				numberOfControls = Convert.ToInt32(groupBoxWeek5.Tag);
				groupBoxWeek5.Controls.Add(createLabel(type, numberOfControls, description));
				groupBoxWeek5.Tag = (numberOfControls+1).ToString();
			}		
			else if (week == 6)
			{
				numberOfControls = Convert.ToInt32(groupBoxWeek6.Tag);
				groupBoxWeek6.Controls.Add(createLabel(type, numberOfControls, description));
				groupBoxWeek6.Tag = (numberOfControls+1).ToString();
			}				
		}
		
		private void ToolStripMenuItemCiclismClick(object sender, EventArgs e)
		{
			
			showPanelWorkout(Constants.BIKE);
		}
		
		private void kryptonLabelTal_Click(object sender, EventArgs e)
		{
			KryptonLabel tmp = (KryptonLabel)sender;
			
			GroupBox aux = (GroupBox)tmp.Parent;
			
			int index = Convert.ToInt32(aux.Tag);
			
			currentDate = workoutDays[index].date;
			
			//MessageBox.Show(currentDate.ToString("dd/MM/yyyy"));
			
			contextMenuStrip1.Show(tmp, tmp.Location);
		}
		
		private void showPanelWorkout(int type)
		{
			newEvent();
			pnlWorkout.Visible = true;
			//TODO: Centralizar panel
		}
		
		private void BtnOKClick(object sender, EventArgs e)
		{
			setClass();
			
			if (demarcation.save())
			{
				pnlWorkout.Visible= false;
			}
			else
			{
				MessageBox.Show("Erro: "+demarcation.errorMessage);
			}
		}
		
	   private void setClass()
		{
			//demarcation.id=		txtdemarcation_id.Text;
			demarcation.user_id=		((cmbUser.SelectedItem) as User).id;
			demarcation.date=		txtdemarcation_date.Text;
			//demarcation.create_date=		txtdemarcation_create_date.Text;
			demarcation.place=		txtdemarcation_place.Text;
			demarcation.meters=		txtdemarcation_meters.Text;
			demarcation.time=		txtdemarcation_hours.Text+":"+txtDemarcationMinutes.Text+":"+txtDemarcationSeconds.Text;
			demarcation.speed=		txtdemarcation_speed.Text;
			demarcation.mark=		txtdemarcation_mark.Text;
			demarcation.fc=		txtdemarcation_fc.Text;
			demarcation.obs=		txtdemarcation_obs.Text;

			if (demarcation.id.Equals(string.Empty))
				demarcation.create_date = DateTime.Now.ToString("yyyy/MM/dd");
		}
		
		private void setEdits()
		{
			//demarcation.id = dgvMain.CurrentRow.Cells["demarcation_id"].Value.ToString();
			
			if (demarcation.get())
			{
				//txtdemarcation_id.Text=		demarcation.id;
				if (listUser.search(demarcation.user_id))
					cmbUser.SelectedIndex = listUser.getIndex();
				
				txtdemarcation_date.Text=		demarcation.date;
				//txtdemarcation_create_date.Text=		demarcation.create_date;
				txtdemarcation_place.Text=		demarcation.place;
				txtdemarcation_meters.Text=		demarcation.meters;
				txtdemarcation_speed.Text=		demarcation.speed;
				txtdemarcation_mark.Text=		demarcation.mark;
				txtdemarcation_fc.Text=		demarcation.fc;
				txtdemarcation_obs.Text=		demarcation.obs;
				
				txtdemarcation_hours.Text=		demarcation.time.Split(':')[0];
				txtDemarcationMinutes.Text=	demarcation.time.Split(':')[1];
				txtDemarcationSeconds.Text=	demarcation.time.Split(':')[2];
			}
		}	
		
		private void newEvent()
		{
			demarcation.clear();
			
			if (listUser.count() > 0)
				cmbUser.SelectedIndex = 0;
			
			txtdemarcation_place.Clear();
			txtdemarcation_date.Text = currentDate.ToString("dd/MM/yyyy");
			txtdemarcation_fc.Text	 = "0-0";
			txtdemarcation_meters.Text = "0.000";
			txtdemarcation_hours.Text   = "00";
			txtDemarcationMinutes.Text  = "00";
			txtDemarcationSeconds.Text  = "00";
			txtdemarcation_obs.Clear();
			
			txtdemarcation_place.Focus();
		}		
		
		void Button1Click(object sender, EventArgs e)
		{
			pnlWorkout.Visible = false;
		}
		
		void ContextMenuStrip1Opening(object sender, System.ComponentModel.CancelEventArgs e)
		{
		
		}
	}
}
